package com.weaver.qfengx;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * edc相关工具类
 * Auther: Qfengx
 * Date: 2023/8/30 下午3:10
 */
public class EdcUtils {

    /**
     * 根据sheetId，x,y坐标系返回对应的Cell数据
     * PS.数据中心的0起始对应表格中的1, 且x,y坐标与常规是相关的
     * --------> y
     * |
     * |
     * | x
     * 未找到返回null
     */
    public static Map<String, String> getCellByCoord(String sheetId, int x, int y) {
        return getCellByCoord(Lists.newArrayList(Maps.newHashMap()), x, y);
    }

    private static boolean checkIsCell(int cx, int cy, int xCount, int yCount, int targetx, int targety) {
        // 计算 区域的x y 区间
        int mX = cx + xCount - 1;
        int mY = cy + yCount - 1;
        if (targetx <= mX && targetx >= cx && targety <= mY && targety >= cy)
            return true;
        return false;
    }

    public static Map<String, String> getCellByCoord(List<Map<String, String>> cellDataList, int x, int y) {
        for (Map<String, String> cellData : cellDataList) {
            int cx = NumberUtils.parseInt(cellData.get("x"), 0);
            int xCount = NumberUtils.parseInt(cellData.get("xCount"), 1);
            int yCount = NumberUtils.parseInt(cellData.get("yCount"), 1);
            int cy = NumberUtils.parseInt(cellData.get("y"), 0);
            if (checkIsCell(cx, cy, xCount, yCount, x, y)) {
                return cellData;
            }
        }
        return null;
    }

    /**
     * 获取cell 的数据值
     * Const 常量，Field 字段（未知），Formula（公式），DateTime (日期时间)，SequenceNumber（序号）
     */
    public static String getCellValue(String uuid) {
        Map<String, String> cellMap = DaoUtils.executeQueryToMap("select * from edc_reportsheetfield where uuid = ?", uuid);
        if (Objects.isNull(cellMap) || cellMap.isEmpty()) {
            return null;
        }
        String cellType = StringUtils.val(cellMap.get("cellType"), "");
        String cellText = StringUtils.val(cellMap.get("cellText"), "");
        String dataSetId = StringUtils.val(cellMap.get("dataSetId"), "");
        // 常量值类型
        if (StringUtils.equalsIgnoreCase(cellMap.get("cellType"), "Const")) {
            return StringUtils.val(cellMap.get("cellText"), "");
        }
        // 字段类型
        if (StringUtils.equalsIgnoreCase(cellMap.get("cellType"), "Field")) {

            return StringUtils.val(cellMap.get("cellText"), "");
        }
        return "";
    }

    public static void main(String[] args) {
        Map<String, String> map = Maps.newHashMap();
        map.put("cellType", "Const");
        map.put("cellText", "cellText");
        map.put("x", "1");
        map.put("y", "0");
        map.put("xCount", "2");
        map.put("yCount", "1");
        map.put("uuid", "1");
        map.put("name", "1");
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 0, 0)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 1, 1)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 1, 2)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 0, 0)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 1, 0)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 2, 0)));
        System.out.println(JSON.toJSONString(getCellByCoord(Lists.newArrayList(map), 3, 0)));
    }

}
